#!/bin/sh


# last cpu total time
total=0

# last idle time
idle=0

# output
out="/sdcard/mcpu_idle"

output(){
    #echo "in param: idle = $1"
    echo "$1" > $out
}

once(){
    # get first line string of /proc/stat
    str=`head -n 1 /proc/stat`
    aa=($str)

    #echo "str=$str"
    idle_cur=${aa[4]}
    total_cur=$(expr ${aa[1]} + ${aa[2]} + ${aa[3]} + ${aa[4]} + ${aa[5]} + ${aa[6]} + ${aa[7]} + ${aa[8]} + ${aa[9]} + ${aa[10]})
    #echo "idle_cur=$idle_cur, total_cur=$total_cur"

    idle_delta=$(expr $idle_cur - $idle)
    total_delta=$(expr $total_cur - $total)

    idle_delta=$(expr $idle_delta \* 100)

    #echo "idle_delta=$idle_delta, total_delta=$total_delta"

    idle_one=$(echo "scale=2; $idle_delta / $total_delta" | bc)

    #echo ">>> idle_one=$idle_one"

    output $idle_one
    
    idle=$idle_cur
    total=$total_cur
: <<EOF
    total_cur=
    idle_cur=

    idle_one=(idle_cur-idle) / (total_cur-total)
        
    to_output idle_one

EOF


    #echo "------------------"
}

while true
do
    once
    sleep 1
done    
